#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<long long, string>> ss;
  vector<pair<long long, string>> hs;
  for (int i = 0; i < n + m; i ++) {
    string name;
    char ch;
    long long w;
    cin >> name >> ch >> w;
    if (ch == 'S') ss.emplace_back(w, name);
    else hs.emplace_back(w, name);
  }
  sort(ss.begin(), ss.end());
  sort(hs.begin(), hs.end());
  cout << hs[ hs.size() - 3 ].second << endl;
  // size_t
  // i = 0  0 >= 0
  // i = -1   -1 >= 0
  for (int i = ss.size() - 6; i >= (int)ss.size() - 9; i --) {
    cout << ss[i].second << endl;
  }
}